a<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="6" :xs="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>个人信息</span>
          </div>
          <div>
            <div class="text-center">
              <userAvatar />
            </div>
            <ul class="list-group list-group-striped">
              <li class="list-group-item">
                <svg-icon icon-class="user" />用户名称
                <div class="pull-right">{{ user.userName }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="phone" />手机号码
                <div class="pull-right">{{ user.phonenumber }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="email" />用户邮箱
                <div class="pull-right">{{ user.email }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="tree" />所属部门
                <div class="pull-right" v-if="user.dept">{{ user.dept.deptName }} / {{ postGroup }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="peoples" />所属角色
                <div class="pull-right">{{ roleGroup }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="date" />创建日期
                <div class="pull-right">{{ user.createTime }}</div>
              </li>
              <li class="list-group-item">
               <div style="text-align: center">
                 <el-button type="primary">
                   生成简历模板
                 </el-button>
                 <el-button @click="clickEditCV">
                   编辑简历模板
                 </el-button>
               </div>
              </li>
            </ul>
          </div>
        </el-card>
      </el-col>
      <el-col :span="18" :xs="24">
        <el-card>
          <div slot="header" class="clearfix">
            <span>基本资料</span>
          </div>
          <el-tabs v-model="activeTab">
            <el-tab-pane label="基本资料" name="userinfo">
              <userInfo :user="user" />
            </el-tab-pane>
            <el-tab-pane label="修改密码" name="resetPwd">
              <resetPwd />
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog :visible.sync="openEditCurriculumVitae" title="编辑简历模板" width="70%">
      <div>
        <el-row>
          <el-col :span="18">
            <el-form ref="form" :model="form">
              <el-form-item label="个人简介" prop="myself"/>
              <editor :height="150" :min-height="30" :value="form.myself"/>
              <el-form-item label="我的总结" prop="mySum"/>
              <editor :height="150" :min-height="20" :value="form.mySum"/>
              <el-form-item label="我的获奖(勾选显示)"/>
              <el-table :data="myCurriculmVitaeWorkAwardList" ref="myCurriculmVitaeWorkAwardTable">
                <el-table-column label="选择获奖"  prop="showStatus">

                    <template slot-scope="scope">
                      <el-switch :inactive-value="0" :active-value="1" v-model="scope.row.showStatus"></el-switch>
                    </template>

                </el-table-column>
                <el-table-column label="索引" type="index"></el-table-column>
                <el-table-column label="获奖名称" prop="awardName"></el-table-column>
                <el-table-column label="获奖日期" prop="awardDate"></el-table-column>
                <el-table-column label="获奖等级" prop="awardGrade">
                  <template slot-scope="scope">
                    <span v-if="scope.row.awardGrade==1">一等奖</span>
                    <span v-else-if="scope.row.awardGrade==2">二等奖</span>
                    <span v-else-if="scope.row.awardGrade==2">三等奖</span>
                  </template>
                </el-table-column>

              </el-table>
              <el-form-item label="我的项目"/>
              <el-row>
                <el-col :span="3">
                  <el-button :icon="'el-icon-plus'" size="mini" type="primary" @click="handleAddMyWorkProject">
                    新增
                  </el-button>
                </el-col>
                <el-col :span="3">
                  <el-button :icon="'el-icon-delete'" size="mini" style="background-color: red;color: white" @click="handleDeleteMyWorkProject">
                    删除
                  </el-button>
                </el-col>
              </el-row>
              <el-table :data="myCurriculumVitaeWorkProjectList" :row-class-name="rowMyCurriculumVitaeWorkProjectIndex"  @selection-change="handleMyCurriculumVitaeWorkProjectSelectionChange" ref="myCurriculumVitaeWorkProject">
                <el-table-column label="选择项目" type="selection"></el-table-column>
                <el-table-column label="索引" type="index"></el-table-column>
                <el-table-column label="项目名称" prop="workProjectName">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.workProjectName"></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="我担任的角色" prop="myRole">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.myRole"></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="项目开始时间" prop="startDate">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.startDate"></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="项目结束时间" prop="endDate">
                  <template slot-scope="scope">
                    <el-input v-model="scope.row.endDate"></el-input>
                  </template>
                </el-table-column>
              </el-table>

            </el-form>
          </el-col>

        </el-row>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import userAvatar from "./userAvatar";
import userInfo from "./userInfo";
import resetPwd from "./resetPwd";
import { getUserProfile } from "@/api/system/user";
import {getMyCurriculumVitae, updateMyCurriculumVitae} from "@/api/system/myCurriculumVitae";
import {listAward} from "@/api/work/award";

export default {
  name: "Profile",
  components: { userAvatar, userInfo, resetPwd },
  data() {
    return {
      user: {},
      roleGroup: {},
      postGroup: {},
      activeTab: "userinfo",
      queryParamss: {
        pageNum: 1,
        pageSize: 10,
        myself: null,
        mySum: null,
        // myCurriculmVitaeWorkAwardList: {
        //   createdTime: null,
        //   updateTime: null,
        //   curriculumVitaeId: null,
        //   id: null,
        //   remark: null,
        //   workAwardId: null,
        // },
        // myCurriculumVitaeWorkProjectList: {
        //   workProjectName: null,
        //   myRole: null,
        //   theCity: null,
        //   startDate: null,
        //   endDate: null,
        //   experienceDescription: null,
        //   createTime: null,
        //   updateTime: null,
        //   curriculumVitaeId: null,
        // }
      },
      // 我的简历信息
      myCurriculumVitae: null,
      // 我的获奖列表
      myCurriculmVitaeWorkAwardList: null,
      // 我的项目列表
      myCurriculumVitaeWorkProjectList: null,

      //用户id
      userId:null,

      openEditCurriculumVitae: false,
      form: {
        //
        // myself: null,
        // mySum: null,
        // myCurriculmVitaeWorkAwardList: {
        //   createdTime: null,
        //   updateTime: null,
        //   curriculumVitaeId: null,
        //   id: null,
        //   remark: null,
        //   workAwardId: null,
        // },
        // myCurriculumVitaeWorkProjectList: {
        //   workProjectName: null,
        //   myRole: null,
        //   theCity: null,
        //   startDate: null,
        //   endDate: null,
        //   experienceDescription: null,
        //   createTime: null,
        //   updateTime: null,
        //   curriculumVitaeId: null,
        // }

      }
    };
  },
  created() {
    // this.getUser();
  },
  mounted() {
    this.getUser();
  },
  methods: {
    async getUser() {

      let response=await getUserProfile();
      // 测试
      console.log(response)
      this.userId = response.data.userId;
      // 获取当前用户简历
      let myCurriculumVitae = (await getMyCurriculumVitae(response.data.userId)).data;
      this.myCurriculumVitae = myCurriculumVitae;
      // 获取全部获奖列表
      let workAward = (await listAward({userId: response.data.userId})).rows;


      console.log("获奖列表", workAward)
      console.log(myCurriculumVitae)

      // 赋值当前用户简历获奖列表和工作列表
      this.myCurriculmVitaeWorkAwardList = workAward;
      this.myCurriculumVitaeWorkProjectList = myCurriculumVitae.myCurriculumVitaeWorkProjectList;
      // // 测试
      // console.log(this.myCurriculmVitaeWorkAwardList, 123)
      //
      // //初始化获奖选择框
      // // 初始化获奖选择框
      // console.log(this.myCurriculmVitaeWorkAwardList,"kong")
      // this.$nextTick(()=>{
      //   this.myCurriculmVitaeWorkAwardList.forEach(item => {
      //     // 如果showStatus状态为1则勾选多选框
      //     console.log(item,"for")
      //     if (item.showStatus == 1) {
      //       this.$refs.myCurriculmVitaeWorkAwardTable.toggleRowSelection(item, true);
      //     }
      //   });
      // })



      this.user = response.data;
      this.roleGroup = response.roleGroup;
      this.postGroup = response.postGroup;
    },
    clickEditCV(){
      this.openEditCurriculumVitae=true;

    },
    handleAddMyWorkProject() {
      this.myCurriculumVitaeWorkProjectList.push({
        workProjectName: null,
        theCity: null,
        startDate: null,
        endDate: null,
        myRole: null,
        experienceDescription: null,
        curriculumVitaeId: this.myCurriculumVitae.id
      })
    },
    handleDeleteMyWorkProject(){
      if (this.checkedMyCurriculumVitaeWorkProject.length == 0) {
        this.$modal.msgError("请先选择要删除的项目数据");
      }else {
        const myCurriculumVitaeWorkProjectList = this.myCurriculumVitaeWorkProjectList;
        const checkedMyCurriculumVitaeWorkProject = this.checkedMyCurriculumVitaeWorkProject;
        this.myCurriculumVitaeWorkProjectList = myCurriculumVitaeWorkProjectList.filter(function(item) {
          return checkedMyCurriculumVitaeWorkProject.indexOf(item.index) == -1
        });
      }
    },

    /** 复选框选中数据 */
    handleMyCurriculumVitaeWorkProjectSelectionChange(selection) {
      this.checkedMyCurriculumVitaeWorkProject = selection.map(item => item.index)
    },
    /** 我的项目序号 */
    rowMyCurriculumVitaeWorkProjectIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        console.log(valid)
        if (valid) {
          this.form.myCurriculumVitaeWorkProjectList = this.myCurriculumVitaeWorkProjectList;
          if (this.form.id != null) {
            updateMyCurriculumVitae(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    reset() {
      this.form = {
        id: null,
        myself: null,
        mySum: null
      };
      this.myCurriculumVitaeWorkProjectList = [];
      this.resetForm("form");
    },



  }
};
</script>
